package com.nycreativity.elecreativity.controller;


import com.nycreativity.elecreativity.aop.ValidateAspect
import com.nycreativity.elecreativity.entity.ResultData
import com.nycreativity.elecreativity.entity.cons.CommonURLConstants
import com.nycreativity.elecreativity.entity.param.address.AddressAddParam
import com.nycreativity.elecreativity.entity.param.address.AddressUpdateParam
import com.nycreativity.elecreativity.entity.po.Address
import com.nycreativity.elecreativity.service.AddressService
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.validation.BindingResult
import org.springframework.web.bind.annotation.*
import javax.annotation.Resource
import javax.validation.Valid

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-03-10
 */
@RestController
@RequestMapping(CommonURLConstants.ADDRESS_PREFIX)
@Api(tags = ["地址相关接口"])
class AddressController {

    @Resource
    lateinit var addressService: AddressService

    @GetMapping
    @ApiOperation("获取用户的收货地址")
    fun getAddress(userId: Long): ResultData<List<Address>> {

        return ResultData.SUCCESS(data = addressService.queryByUserId(userId))

    }

    @PostMapping
    @ApiOperation("添加用户的收货地址")
    @ValidateAspect.ParamValidate
    fun add(@RequestBody @Valid addressAddParam: AddressAddParam, bindingResult: BindingResult): ResultData<Any>{

        val res = addressService.add(addressAddParam)
        return if (res){
            ResultData.SUCCESS()
        }else{
            ResultData.FAILED("添加失败，请检查参数是否正确")
        }

    }

    @PutMapping
    @ApiOperation("修改用户收获地址")
    @ValidateAspect.ParamValidate
    fun updateById(@RequestBody @Valid addressUpdateParam: AddressUpdateParam, bindingResult: BindingResult): ResultData<Any>{
        val res = addressService.updateById(addressUpdateParam)
        return if (res){
            ResultData.SUCCESS()
        }else{
            ResultData.FAILED("修改失败，请检查参数是否正确")
        }
    }

    @DeleteMapping
    @ApiOperation("根据id删除用户收获地址")
    fun deleteById(id: Long): ResultData<Any>{
        addressService.deleteById(id)
        return ResultData.SUCCESS()
    }

}
